package com.julang.check.bean;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import static com.julang.check.utils.CommonData.FIVEOFNUMBER;
import static com.julang.check.utils.CommonData.FOUROFNUMBER;
/**
 * @author 赵自强
 * */
@ApiModel(value = "RepeatWord", description = "可替换词语相关信息")
@Data
public class RepeatWord {
    @ApiModelProperty(value = "原词语")
    private String orginWord;
    @ApiModelProperty(value = "这个词语重复的次数")
    private int repeatTime;
    @ApiModelProperty(value = "原词语重复占比")
    private double repeatPercent;
    @ApiModelProperty(value = "替换的词语")
    private String replaceWord = "";
    @ApiModelProperty(value = "是否可以替换 默认为否")
    private boolean canPlace = false;
    @ApiModelProperty(value = "包含此重复词的原句id")
    private String orignSentenceId = "";

    @Override
    public boolean equals(Object word) {
        return orginWord.equals(((RepeatWord) word).getOrginWord());
    }

    public void convertCanReplace(String replaceWord) {
        this.replaceWord = replaceWord;
        if (StringUtils.isEmpty(replaceWord)) {
            this.canPlace = false;
        } else {
            this.canPlace = true;
        }
    }

    public RepeatWord(String orginWord, int repeatTime, double repeatPercent,String orignSentenceId) {
        this.orginWord = orginWord;
        this.repeatTime = repeatTime;
        this.repeatPercent = repeatPercent;
        this.orignSentenceId = orignSentenceId;
    }

    public RepeatWord(String orginWord, int repeatTime, double repeatPercent, String replaceWord,String orignSentenceId) {
        this.orginWord = orginWord;
        this.repeatTime = repeatTime;
        this.repeatPercent = repeatPercent;
        this.replaceWord = replaceWord;
        this.orignSentenceId = orignSentenceId;
        if (StringUtils.isEmpty(replaceWord)) {
            this.canPlace = false;
        } else {
            this.canPlace = true;
        }
    }

    public String description() {
        return repeatTime + "-----&&------" + repeatPercent + "-----&&------" + orginWord + "-----&&------" + orignSentenceId+ "-----&&------" + replaceWord;
    }



    public static RepeatWord build(String data) {
        String[] s = data.split("-----&&------");
        int i;
        double j;
        try {
            i = Integer.valueOf(s[0]);
            j = Float.valueOf(s[1]);
        } catch (NumberFormatException e) {
            return null;
        }
        if (s.length == FIVEOFNUMBER) {
            return new RepeatWord(s[2], i, j, s[4], s[3]);
        } else if (s.length == FOUROFNUMBER) {
            return new RepeatWord(s[2], i, j, s[3]);
        } else {
            return null;
        }
    }

    public static void main(String[] args) {
        RepeatWord r = RepeatWord.build("13-----&&------0.042763158679008484-----&&------六分仪-----&&------1,2,3-----&&------四分仪");
        RepeatWord r1 = RepeatWord.build("15-----&&------152.455641-----&&------dsadada-----&&------1,2,3-----&&------");
        System.out.println(r.description());
        System.out.println(r.toString());
        System.out.println(r1.description());
        System.out.println(r1.toString());
    }
}
